<template>
	<a-form :ref="'ruleForm'" :model="formState" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
		<a-row :gutter="[10, 10]">
			<a-col :span="24">
				<a-radio-group
					v-model:value="formState.dataSetConfig.dataSetType"
					optionType="button"
					buttonStyle="solid"
					:options="[
						{ label: '静态', value: 0 },
						{ label: '数据源', value: 1 }
					]"
				>
				</a-radio-group>
			</a-col>

			<template v-if="formState.dataSetConfig.dataSetType == 0">
				<a-col :span="24">
					<JsonEditor v-model="formState.data.listValue" />
				</a-col>
			</template>
			<template v-else>
				<a-col :span="24">
					<SelectTree @onChange="selectTreeChange" v-model:paramsJson="formState.dataSetConfig.dataSetJson" />
				</a-col>
				<a-col :span="24">
					<a-form-item label="标题" name="title" class="form-item">
						<a-select
							style="width: 100%"
							placeholder="请选择数据源"
							v-model:value="formState.data.mainContentKey.field"
							optionLabelProp="label"
						>
							<a-select-option
								v-for="optionItem in dataOption"
								:value="optionItem"
								:key="optionItem"
								:label="optionItem"
							>
								{{ optionItem }}
							</a-select-option>
						</a-select>
						<!-- <a-input
							v-if="formState.data.mainContentKey.field"
							class="mt-[10px]"
							v-model:value="formState.data.mainContentKey.alias"
							placeholder="字段别名"
						>
							<template #addonBefore>
								<div class="field-prefix">
									<a-tag color="blue">{{ formState.data.mainContentKey.field }}</a-tag>
								</div>
							</template>
						</a-input> -->
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="副标题" name="title" class="form-item">
						<a-select
							style="width: 100%"
							placeholder="请选择数据源"
							v-model:value="formState.data.subContentKey.field"
							optionLabelProp="label"
						>
							<a-select-option
								v-for="optionItem in dataOption"
								:value="optionItem"
								:key="optionItem"
								:label="optionItem"
							>
								{{ optionItem }}
							</a-select-option>
						</a-select>
						<!-- <a-input
							v-if="formState.data.subContentKey.field"
							class="mt-[10px]"
							v-model:value="formState.data.subContentKey.alias"
							placeholder="字段别名"
						>
							<template #addonBefore>
								<div class="field-prefix">
									<a-tag color="blue">{{ formState.data.subContentKey.field }}</a-tag>
								</div>
							</template>
						</a-input> -->
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="状态" name="title" class="form-item">
						<a-select
							style="width: 100%"
							placeholder="请选择数据源"
							v-model:value="formState.data.status.field"
							optionLabelProp="label"
						>
							<a-select-option
								v-for="optionItem in dataOption"
								:value="optionItem"
								:key="optionItem"
								:label="optionItem"
							>
								{{ optionItem }}
							</a-select-option>
						</a-select>
						<!-- <a-input
							v-if="formState.data.status.field"
							class="mt-[10px]"
							v-model:value="formState.data.status.alias"
							placeholder="字段别名"
						>
							<template #addonBefore>
								<div class="field-prefix">
									<a-tag color="blue">{{ formState.data.status.field }}</a-tag>
								</div>
							</template>
						</a-input> -->
					</a-form-item>
				</a-col>
			</template>
		</a-row>
	</a-form>
</template>

<script setup>
	import { ref, computed } from 'vue'
	import JsonTree from '@/views/plugin/dashboard/plugin/public/JsonTree/JsonTree.vue'
	import JsonEditor from '@/views/plugin/dashboard/plugin/public/JsonEditor/JsonEditor.vue'
	import DataSet from '@/views/plugin/dashboard/plugin/public/DataSet/DataSet.vue'
	import SelectTree from '@/views/plugin/dashboard/plugin/public/SelectTree/SelectTree.vue'
	import datasetApi from '@/views/plugin/dashboard/api/datasetApi'
	import { designStore } from '@/views/plugin/dashboard/store'
	const useDesignStore = designStore()
	// -----------------------------------------------------------
	import config from '../config'
	import { reactive, defineProps, defineEmits, onMounted } from 'vue'
	import { cloneDeep } from 'lodash-es'
	const emits = defineEmits(['upDataConfig'])
	const props = defineProps({
		config: {
			type: Object,
			default: () => {
				return cloneDeep(config.data)
			}
		}
	})

	// 静态
	const staticData =
		props.config.dataSetConfig.dataSetType === 1 ? ref(cloneDeep(config.data)) : ref(cloneDeep(props.config.data))
	// 动态
	const dynamicData =
		props.config.dataSetConfig.dataSetType === 0 ? ref(cloneDeep(config.data)) : ref(cloneDeep(props.config.data))

	const formState = computed({
		get() {
			const dataSetConfig = props.config.dataSetConfig // 更新的状态
			// 初始值
			const dataSource = dataSetConfig.dataSetType === 1 ? dynamicData.value : staticData.value
			return {
				data: dataSource,
				dataSetConfig: dataSetConfig
			}
		},
		set(value) {
			if (value.dataSetConfig.dataSetType === 1) {
				dynamicData.value = value.data
			} else {
				staticData.value = value.data
			}
		}
	})
	// 数据变化清空下拉
	const selectTreeChange = () => {
		formState.value.data.mainContentKey.field = undefined
		formState.value.data.subContentKey.field = undefined
		formState.value.data.status.field = undefined
	}

	// 数据源
	const dataSource = ref([])
	// 模拟接口返回下拉数据
	const dataOption = ref([])
	// 获取下拉数据
	watch(
		() => formState.value.dataSetConfig.dataSetJson,
		(newVal) => {
			if (props.config.dataSetConfig.dataSetType === 1) {
				const list = useDesignStore.dataSource || []
				let datasetId = ''
				let indicatorId = ''
				list.forEach((parent) => {
					parent.config.indicators.forEach((child) => {
						if (child.id === newVal.id) {
							datasetId = parent.id
							indicatorId = child.id
						}
					})
				})
				if (datasetId && indicatorId) {
					// 下拉数据
					datasetApi.querySelectedColumns({ datasetId, indicatorId }).then((res) => {
						dataOption.value = res || []
					})
					datasetApi.preview(newVal).then((res) => {
						dataSource.value = res || []
						formState.value.data.listValue.value = res || []
					})
				}
			}
		},
		{
			immediate: true,
			deep: true
		}
	)

	// 更新组件内容
	watch(
		() => formState.value,
		(newVal) => {
			formState.value = newVal
			emits('upDataConfig', { ...props.config, ...newVal })
		},
		{
			deep: true
		}
	)
</script>
<style scoped>
	:deep(.ant-radio-button-wrapper) {
		padding: 0px;
		width: 100%;
	}
	:deep(.ant-radio-group) {
		display: flex;
		text-align: center;
	}
	:deep(.form-item) {
		margin-bottom: 0px;
	}
</style>
